package com.jgp.sys.ui;

import com.jgp.sys.utils.ResultUtil;

import java.util.*;

/**
 * 项目   sys
 * 作者   loufei
 * 时间   2018/3/5
 */
public class Result {
    private Boolean flag;
    private Map<String,Object> data;
    private Map<String,Object> extMap;
    private List<String> msgs ;
    private List<Error> errors;
    private List<ValidError> validErrors;

    public Result(Map<String, Object> data) {
        this.flag = true;
        this.data = data;
        this.extMap = new HashMap<>();
        this.msgs = new ArrayList<>();
    }

    public Result() {
        this.flag = false;
        this.extMap = new HashMap<>();
        this.data = new HashMap<>();
        this.msgs = new ArrayList<>();
    }

    public Result(Boolean flag) {
        this.flag = flag;
        this.extMap = new HashMap<>();
        this.data = new HashMap<>();
        this.msgs = new ArrayList<>();
    }

    public Result(Boolean flag, Map<String,Object> extMap) {
        this.flag = flag;
        this.msgs = new ArrayList<>();
        this.extMap = extMap;
        this.data = new HashMap<>();
    }

    public Result(Boolean flag, List<String> msgs ,Map<String,Object> extMap) {
        this.flag = flag;
        this.msgs = msgs;
        this.extMap = extMap;
        this.data = new HashMap<>();
    }

    public Result addData(String alias,Object object){
        this.data.put(alias,object);
        return this;
    }

    public Result addAllData(Map<String,Object> map){
        this.data.putAll(map);
        return this;
    }

    public Result addExt(String alias,Object ext){
        this.extMap.put(alias,ext);
        return this;
    }

    public Result addAllExt(Map<String,Object> extMap){
        this.extMap.putAll(extMap);
        return this;
    }


    public Result addMsg(String msg){
        this.msgs.add(msg);
        return this;
    }

    public Result addAllMsg(List<String> msgs){
        this.msgs.addAll(msgs);
        return this;
    }

    public Result flag(Boolean flag) {
        this.flag = flag;
        return this;
    }

    public Boolean getFlag() {
        return flag;
    }

    public Map<String, Object> getData() {
        return data;
    }
    
    public Object getData(String alias) {
        return data.get(alias);
    }

    public Map<String, Object> getExtMap() {
        return extMap;
    }

    public List<String> getMsgs() {
        return msgs;
    }
    
    public List<Error> getErrors() {
        return errors;
    }
    
    public List<ValidError> getValidErrors() {
        return validErrors;
    }
    
    public Result error(String code, String msg, String module) {
        if(Objects.isNull(this.errors)){
            this.errors = new ArrayList<>();
        }
        errors.add(new Error(code,msg,module));
        return this;
    }
    
    public Result error(Error error) {
        if(Objects.isNull(this.errors)){
            errors = new ArrayList<>();
        }
        errors.add(error);
        return this;
    }
    
    public Result errors(List<Error> errors) {
        if(Objects.isNull(this.errors)){
            this.errors = new ArrayList<>();
        }
        this.errors.addAll(errors);
        return this;
    }
    
    public Result validError(String model,String field,String msg) {
        if(Objects.isNull(this.validErrors)){
            this.validErrors = new ArrayList<>();
        }
        this.validErrors.add(new ValidError(model,field,msg));
        return this;
    }
    
    public Result validError(ValidError error) {
        if(Objects.isNull(this.validErrors)){
            this.validErrors = new ArrayList<>();
        }
        this.validErrors.add(error);
        return this;
    }
    
    public Result validErrors(List<ValidError> errors) {
        if(Objects.isNull(this.validErrors)){
            this.validErrors = new ArrayList<>();
        }
        this.validErrors.addAll(errors);
        return this;
    }
    
    public Result operatorModelExtData() {
        this.data = (Map) ResultUtil.operatorModelExtData(this.data);
        return this;
    }
}
